const chalk = require('chalk')
// const rimraf = require('rimraf')
const { sh } = require('tasksfile')
const pages = require('./pages')

// 模块名，可能为多个
let validPageList = []
let pageList = Object.keys(pages)
let argLast = process.argv[process.argv.length - 1];
if (argLast === '--progress') {
    // 说明是全量
    validPageList = pageList
} else {
    // 单文件或多文件
    validPageList = argLast.split(',').filter((item) => pageList.includes(item))
}
if (!validPageList.length) {
    console.log('*****模块名不正确*******')
    return
}

const count = validPageList.length
let environmentFlage='uat'
if(process.env.NODE_ENV=='production'){
    environmentFlage='pro'
}else{
    environmentFlage=process.env.NODE_ENV
}
let current = 0
// 逐个执行模块编译
let list = []
for (let i = 0; i < validPageList.length; i += 1) {
    const moduleName = validPageList[i]
    process.env.MODULE_NAME = moduleName
    // 环境变量配置
  
    sh(`vue-cli-service build --mode ${environmentFlage} ${moduleName}`, { async: true }).then(() => {
        // console.log(chalk.blue(`${moduleName} 模块编译完成`))
        current += 1
        list.push(moduleName);
        if (current === count) {
            console.log(chalk.green(`[ ${list} ] 模块编译完成，共:${list.length} 个`))
        }
    })
}
